<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="form" label-width="120px" class="demo-ruleForm">
            <el-form-item label="客户名称" prop="custId">
                <el-select v-model="ruleForm.custId" placeholder="请选择">
                    <el-option
                            v-for="item in custInfoList"
                            :key="item.custId"
                            :label="item.cnName"
                            :value="item.custId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="项目名称" prop="proName">
                <el-input  v-model="ruleForm.proName"></el-input>
            </el-form-item>
            <el-form-item label="项目金额" prop="money">
                <el-input-number :controls="false"  v-model="ruleForm.money"></el-input-number>
            </el-form-item>
            <el-form-item label="服务开始时间" prop="beginTm">
                <el-date-picker
                        v-model="ruleForm.beginTm"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="服务结束时间" prop="endTm">
                <el-date-picker
                        v-model="ruleForm.endTm"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="状态" prop="stat">
                <el-select v-model="ruleForm.stat" placeholder="请选择">
                    <el-option
                            v-for="item in statList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>

        </el-form>
    </div>
</template>

<script>
    import {insertProject,getAllCust1}from '@/api/projectApi'
    export default {
        data() {
            return {
                ruleForm: {
                    custId: '',
                    proName: '',
                    money:'',
                    beginTm: '',
                    endTm: '',
                    stat:'',
                },
                custInfoList:[],
                statList:[
                    {
                        value: '0',
                        label: '未开始'
                    },
                    {
                        value: '1',
                        label: '进行中'
                    },
                    {
                        value: '2',
                        label: '已完结'
                    },
                ],
                rules: {
                    custId: [
                        { required: true, message: '客户名称不可为空', trigger: 'blur' },
                    ],
                    proName: [
                        { required: true, message: '项目名称不可为空', trigger: 'blur' },
                    ],
                    money: [
                        { required: true, message: '项目金额不可为空不可为空', trigger: 'change' },
                    ],
                    beginTm: [
                        { required: true, message: '开始日期不可为空不可为空', trigger: 'change' },
                    ],
                    endTm: [
                        { required: true, message: '结束日期不可为空不可为空', trigger: 'change' },
                    ],
                }
            };
        },
        mounted() {
            getAllCust1().then(res=>{
                this.custInfoList=res.data.data
            }).catch(e => {
                this.$message.error({
                    message: '抱歉，操作遇到异常，请重试或联系管理员处理'
                })
            })
        },
        methods: {
            //提交
            submit() {
                return new Promise((resolve, reject) => {
                    this.$refs['form'].validate((valid) => {
                        if(valid){
                            insertProject(this.ruleForm).then(res => {
                                resolve(res.data)
                            }).catch(e => {
                                console.log(e,555)
                                reject(e)
                                this.$message.error({
                                    message: '抱歉，操作遇到异常，请重试或联系管理员处理'
                                })
                            })
                        }else {
                            this.$message.error('业务校验未通过')
                            reject(new Error('业务校验失败'))
                        }
                    })
                })

            }
        }
    }
</script>

<style scoped>

</style>
